package cake.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cake.dao.CakeDao;
import cake.dao.ShoppingCarDao;
import cake.dao.impl.CakeDaoImpl;
import cake.dao.impl.ShoppingCarDaoImpl;
import cake.entity.Cake;
import cake.entity.ShoppingCar;
import cake.entity.User;


@WebServlet("/cartlist")
public class CartListServlet extends HttpServlet {
	private CakeDao cakeDao=new CakeDaoImpl();
	private ShoppingCarDao sc=new ShoppingCarDaoImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain; charset=UTF-8");
		User user=(User)req.getSession().getAttribute("LoginUser");
		
	  if(user!=null) {
			List<Cake> clist=sc.getCakeListFromShopping(user.getId());
			Gson gson = new Gson();
			String json = gson.toJson(clist);
			resp.getWriter().write(json);
	  }else {
			resp.sendRedirect("login.html");
	  }
	}
}
